package com.zyy.study.always.code6;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Book {
    private String title;
    private String author;
    private boolean isBorrowed;//是否被借出，默认否

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
        this.isBorrowed = false;
    }

    public synchronized boolean borrow() {
        if (isBorrowed) {
            System.out.println(title+"已被借阅");
            return false;
        }
        isBorrowed = true;
        System.out.println(title + "借阅成功");
        return true;
    }

    public synchronized boolean returnBook() {
        if (!isBorrowed) {
            System.out.println(title + "未被借阅，无需归还");
            return false;
        }
        isBorrowed = false;
        System.out.println(title + "归还成功");
        return true;
    }
}
